package com.talkyun.openx.server.core;

import com.talkyun.utils.BaseConverter;

import java.util.HashMap;
import java.util.Map;

class AbstractContext {
    private static final BaseConverter CONVERTER = new BaseConverter();
    private Map<String, Object> k2v;
    String sessionId;

    AbstractContext(String sessionId) {
        this.sessionId = sessionId;
        this.k2v = new HashMap<String, Object>(4);
    }

    public String getSessionId() {
        return sessionId;
    }

    public void put(String key, Object val) {
        k2v.put(key, val);
    }

    public void putIfNull(String key, Object val) {
        Object val2 = k2v.get(key);
        if (val2 == null) {
            k2v.put(key, val);
        }
    }

    public boolean exists(String key) {
        return k2v.containsKey(key);
    }

    public Object get(String key) {
        return k2v.get(key);
    }

    public Object getValue(String key) {
        return k2v.get(key);
    }

    public String getString(String key, String def) {
        return this.exists(key) ? CONVERTER.toString(this.getValue(key)) : def;
    }

    public boolean getBoolean(String key, boolean def) {
        return this.exists(key) ? CONVERTER.toBoolean(this.getValue(key)) : def;
    }

    public long getLong(String key, long def) {
        return this.exists(key) ? CONVERTER.toLong(this.getValue(key)) : def;
    }
}